package com.ruoyi.imports.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.excel.EasyExcel;
import com.ruoyi.imports.domain.OperatingExcelData;
import com.ruoyi.imports.domain.ProductionExcelData;
import com.ruoyi.imports.listener.OperatingExcelDataListener;
import com.ruoyi.imports.listener.ProductionExcelDataListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.imports.domain.TdProductionConsume;
import com.ruoyi.imports.service.ITdProductionConsumeService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 药剂消耗明细Controller
 *
 * @author ruoyi
 * @date 2025-07-19
 */
@RestController
@RequestMapping("/consume")
public class TdProductionConsumeController extends BaseController {
    @Autowired
    private ITdProductionConsumeService tdProductionConsumeService;

    /**
     * 查询药剂消耗明细列表
     */
    @RequiresPermissions("imports:consume:list")
    @GetMapping("/list")
    public TableDataInfo list(TdProductionConsume tdProductionConsume) {
        startPage();
        List<TdProductionConsume> list = tdProductionConsumeService.selectTdProductionConsumeList(tdProductionConsume);
        return getDataTable(list);
    }

    /**
     * 导出药剂消耗明细列表
     */
    @RequiresPermissions("imports:consume:export")
    @Log(title = "药剂消耗明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TdProductionConsume tdProductionConsume) {
        List<TdProductionConsume> list = tdProductionConsumeService.selectTdProductionConsumeList(tdProductionConsume);
        ExcelUtil<TdProductionConsume> util = new ExcelUtil<TdProductionConsume>(TdProductionConsume.class);
        util.exportExcel(response, list, "药剂消耗明细数据");
    }

    /**
     * 获取药剂消耗明细详细信息
     */
    @RequiresPermissions("imports:consume:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tdProductionConsumeService.selectTdProductionConsumeById(id));
    }

    /**
     * 新增药剂消耗明细
     */
    @RequiresPermissions("imports:consume:add")
    @Log(title = "药剂消耗明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TdProductionConsume tdProductionConsume) {
        return toAjax(tdProductionConsumeService.insertTdProductionConsume(tdProductionConsume));
    }

    /**
     * 修改药剂消耗明细
     */
    @RequiresPermissions("imports:consume:edit")
    @Log(title = "药剂消耗明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TdProductionConsume tdProductionConsume) {
        return toAjax(tdProductionConsumeService.updateTdProductionConsume(tdProductionConsume));
    }

    /**
     * 删除药剂消耗明细
     */
    @RequiresPermissions("imports:consume:remove")
    @Log(title = "药剂消耗明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tdProductionConsumeService.deleteTdProductionConsumeByIds(ids));
    }


    //    @RequiresPermissions("imports:imports:import")
    @Log(title = "过程数据监测表", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importExcel(@RequestParam("file") MultipartFile file,
                                  @RequestParam(value = "projectId", required = false) Long projectId) {
        try {
            // 1. 创建监听器
            ProductionExcelDataListener listener = new ProductionExcelDataListener();

            // 2. 使用监听器读取 Excel 数据
            EasyExcel.read(file.getInputStream(), ProductionExcelData.class, listener)
                    .headRowNumber(3)
                    .sheet(3) // 读取第一个 sheet
                    .doRead(); // 执行读取

            // 3. 从监听器获取解析后的数据
            List<ProductionExcelData> excelDataList = listener.getDataList();

            // 4. 检查数据是否为空
            if (excelDataList == null || excelDataList.isEmpty()) {
                return error("Excel 文件中未找到有效数据");
            }

            // 5. 调用 Service 处理导入逻辑
            tdProductionConsumeService.importOperatingDataType(excelDataList, projectId);

            return success("导入成功，共导入 " + excelDataList.size() + " 条数据");

        } catch (IOException e) {
            return error("导入失败：文件读取异常，" + e.getMessage());
        } catch (Exception e) {
            return error("导入失败：" + e.getMessage());
        }

    }
}
